<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>syncgrain</title>
    <link rel="stylesheet" type="text/css" href="csound.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.78.1" />
    <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" />
    <link rel="up" href="OpcodesTop.html" title="Orchestra Opcodes and Operators" />
    <link rel="prev" href="svfilter.html" title="svfilter" />
    <link rel="next" href="syncloop.html" title="syncloop" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">syncgrain</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="svfilter.html">Prev</a> </td>
          <th width="60%" align="center">Orchestra Opcodes and Operators</th>
          <td width="20%" align="right"> <a accesskey="n" href="syncloop.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="refentry">
      <a id="syncgrain"></a>
      <div class="titlepage"></div>
      <a id="IndexSyncgrain" class="indexterm"></a>
      <div class="refnamediv">
        <h2>
          <span class="refentrytitle">syncgrain</span>
        </h2>
        <p>syncgrain — 
Synchronous granular synthesis.
          </p>
      </div>
      <div class="refsect1">
        <a id="idp154462992"></a>
        <h2>Description</h2>
        <p>
<span class="emphasis"><em>syncgrain</em></span> implements synchronous granular synthesis. The source sound for the
grains is obtained by reading a function table containing the samples of the source waveform. 
For sampled-sound sources, <a class="link" href="GEN01.html" title="GEN01"><em class="citetitle">GEN01</em></a> is used.
<span class="emphasis"><em>syncgrain</em></span> will accept deferred allocation tables.
  </p>
        <p>
The grain generator has full control of frequency (grains/sec), overall amplitude, 
grain pitch (a sampling increment) and grain size (in secs), 
both as fixed or time-varying (signal) parameters. 
An extra parameter is the grain pointer speed (or rate), 
which controls which position the generator will start reading samples 
in the table for each successive grain. It is measured in fractions of grain size, 
so a value of 1 (the default) will make each successive grain read from where the 
previous grain should finish. A value of 0.5 will make the next grain start at the midway position 
from the previous grain start and finish, etc.. A value of 0 will make the generator read 
always from a fixed position of the table (wherever the pointer was last at).
A negative value will decrement pointer positions. This control gives extra flexibility for creating 
timescale modifications in the resynthesis.
  </p>
        <p>
<span class="emphasis"><em>syncgrain</em></span> will generate any number of parallel grain streams (which will depend
on grain density/frequency), up to the <span class="emphasis"><em>iolaps</em></span> value (default 100). The number
of streams (overlapped grains) is determined by grainsize*grain_freq. More grain overlaps will
demand more calculations and the synthesis might not run in realtime (depending on processor power).
  </p>
        <p>
<span class="emphasis"><em>syncgrain</em></span> can simulate FOF-like formant synthesis, provided that a suitable shape is used 
as grain envelope and a sinewave as the grain wave. For this use, grain sizes of around 
0.04 secs can be used. The formant centre frequency is determined by the grain pitch. 
Since this is a sampling increment, in order to use a frequency in Hz, that value has to be 
scaled by tablesize/sr. Grain frequency will determine the fundamental.
  </p>
        <p>
<span class="emphasis"><em>syncgrain</em></span> uses floating-point indexing, so its precision is not affected by
large-size tables. This opcode is based on the SndObj library SyncGrain class.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp154545360"></a>
        <h2>Syntax</h2>
        <pre class="synopsis">asig <span class="command"><strong>syncgrain</strong></span> kamp, kfreq, kpitch, kgrsize, kprate, ifun1, \
      ifun2, iolaps</pre>
      </div>
      <div class="refsect1">
        <a id="idp154547712"></a>
        <h2>Initialization</h2>
        <p>
      <span class="emphasis"><em>ifun1</em></span> -- source signal function table. Deferred-allocation tables (see
      <a class="link" href="GEN01.html" title="GEN01"><em class="citetitle">GEN01</em></a>) are accepted, but the opcode
      expects a mono source.
    </p>
        <p>
      <span class="emphasis"><em>ifun2</em></span> -- grain envelope function table.
    </p>
        <p>
      <span class="emphasis"><em>iolaps</em></span> -- maximum number of overlaps,
      max(<span class="emphasis"><em>kfreq</em></span>)*max(<span class="emphasis"><em>kgrsize</em></span>). Estimating a large value
      should not affect performance, but exceeding this value will probably have disastrous
      consequences.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp154553552"></a>
        <h2>Performance</h2>
        <p>
      <span class="emphasis"><em>kamp</em></span> -- amplitude scaling
    </p>
        <p>
      <span class="emphasis"><em>kfreq</em></span> -- frequency of grain generation, or density, in grains/sec.
    </p>
        <p>
      <span class="emphasis"><em>kpitch</em></span> -- grain pitch scaling (1=normal pitch, &lt; 1 lower, &gt; 1 higher; negative, backwards)
    </p>
        <p>
      <span class="emphasis"><em>kgrsize</em></span> -- grain size in secs.
    </p>
        <p>
      <span class="emphasis"><em>kprate</em></span> -- readout pointer rate, in grains. The value of 1 will advance 
the reading pointer 1 grain ahead in the source table. Larger values 
will time-compress and smaller values will time-expand the source signal. 
Negative values will cause the pointer to run backwards and zero will 
freeze it.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp154559632"></a>
        <h2>Examples</h2>
        <p>
      Here is an example of the syncgrain opcode. It uses the file <a class="ulink" href="examples/syncgrain.csd" target="_top"><em class="citetitle">syncgrain.csd</em></a>.
      </p>
        <div class="example">
          <a id="idp154561408"></a>
          <p class="title">
            <strong>Example 888. Example of the syncgrain opcode.</strong>
          </p>
          <div class="example-contents">
            <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p>
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
-odac  <span class="comment">;;;realtime audio out</span>
<span class="comment">;-iadc    ;;;uncomment -iadc if realtime audio input is needed too</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o syncgrain.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="ohdr">sr</span> <span class="op">=</span> 44100 
<span class="ohdr">ksmps</span> <span class="op">=</span> 32 
<span class="ohdr">0dbfs</span>  <span class="op">=</span> 1 
<span class="ohdr">nchnls</span> <span class="op">=</span> 2
  
<span class="oblock">instr</span> 1
		
iolaps  <span class="op">=</span> 2
igrsize <span class="op">=</span> 0.04
ifreq   <span class="op">=</span> iolaps<span class="op">/</span>igrsize
ips     <span class="op">=</span> 1<span class="op">/</span>iolaps

istr    <span class="op">=</span> .3   <span class="op">/</span><span class="op">*</span> timescale  <span class="op">*</span><span class="op">/</span>
ipitch  <span class="op">=</span> p4   <span class="op">/</span><span class="op">*</span> pitchscale <span class="op">*</span><span class="op">/</span>

asig	<span class="opc">syncgrain</span> 1, ifreq, ipitch, igrsize, ips<span class="op">*</span>istr, 1, 2, iolaps
	<span class="opc">outs</span>	  asig, asig 

<span class="oblock">endin</span>
<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>
<span class="stamnt">f</span>1 0 0 1 "<span class="stamnt">f</span>ox.wav" 0 0 0	<span class="comment">;deferred table</span>
<span class="stamnt">f</span>2   0   8192   20   2   1

<span class="stamnt">i</span>1 0 5 1
<span class="stamnt">i</span>1 + 5 4
<span class="stamnt">i</span>1 + 5 .8
<span class="stamnt">e</span>
<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />
    </p>
      </div>
      <div class="refsect1">
        <a id="idp154565696"></a>
        <h2>Credits</h2>
        <p>
      </p>
        <table border="0" summary="Simple list" class="simplelist">
          <tr>
            <td>Author: Victor Lazzarini</td>
          </tr>
          <tr>
            <td>January 2005</td>
          </tr>
        </table>
        <p>
    </p>
        <p>New plugin in version 5</p>
        <p>January 2005.</p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="svfilter.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="OpcodesTop.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="syncloop.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">svfilter </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> syncloop</td>
        </tr>
      </table>
    </div>
  </body>
</html>
